﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StudentApartMentManage
{
    public partial class StuInfoModify : Form
    {
        public StuInfoModify()
        {
            InitializeComponent();
        }
        void showinfo()
        {
            textBox1.Text = dataGridView1.SelectedCells[0].Value.ToString();
            textBox2.Text = dataGridView1.SelectedCells[1].Value.ToString();
            string str = dataGridView1.SelectedCells[2].Value.ToString();
            if(str.Equals("男"))
            {
                radioButton1.Checked = true;
            }
            else
            {
                radioButton2.Checked = true;
            }
            comboBox1.Text = dataGridView1.SelectedCells[3].Value.ToString();
            comboBox2.Text = dataGridView1.SelectedCells[4].Value.ToString();
            textBox3.Text = dataGridView1.SelectedCells[5].Value.ToString();
            comboBox3.Text = dataGridView1.SelectedCells[6].Value.ToString();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string str = dataGridView1.SelectedCells[1].Value.ToString();
                if (MessageBox.Show("确定要修改学生" + str + "的信息吗?", "警告", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    string sql = string.Format("update 学生信息表 set 学号='{0}',姓名='{1}',性别='{2}',学院='{3}',专业='{4}',班级='{5}',民族='{6}' where 学号='{7}'", textBox1.Text.Trim(),textBox2.Text.Trim(),radioButton1.Checked?"男":"女",comboBox1.Text.Trim(),comboBox2.Text.Trim(),textBox3.Text.Trim(),comboBox3.Text.Trim(),dataGridView1.SelectedCells[0].Value.ToString());
                    int i = sqlHelper.Execute(sql);
                    if (i != -1)
                    {
                        MessageBox.Show("修改学生信息成功!");
                        dataGridView1.DataSource = DataShow.show("select 学号, 姓名, 性别, 学院, 专业, 班级, 民族 from 学生信息表");
                    }
                    else
                        MessageBox.Show("修改学生信息失败!");
                }


            }
            catch (Exception ex)
            {
                MessageBox.Show("修改失败!");
                return;
            }

            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string sql0 = string.Format("select 学号,姓名,性别,学院,专业,班级,民族 from 学生信息表 where 学号='{0}'", textBox4.Text.Trim());
            dataGridView1.DataSource = DataShow.show(sql0);
        }

        private void StuInfoModify_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = DataShow.show("select 学号, 姓名, 性别, 学院, 专业, 班级, 民族 from 学生信息表");
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            showinfo();
        }
    }
}
